<?php

namespace Maestros\Controller\Service;

use Zend\Db\Adapter\Adapter;
use Componentes\Controller\UtilsArrayController;
use Maestros\Model\Entity\Pensum;
use Maestros\Controller\Service\GenericService;
use Maestros\Model\Dao\PensumDao;
use Maestros\Controller\Service\MateriaEjeService;
use Maestros\Controller\Service\PeriodoFaseService;
use Maestros\Controller\Service\TrayectoSemestreService;
use Maestros\Controller\Service\MateriaService;
use Inscripcion\Controller\Service\PensumCursarService;

class PensumService extends GenericService {
    
     /**
     * @var Zend\Db\Adapter\Adapter 
     */
    private $dbAdapter;
    private static $pensumService;

    /**
     * @param \Zend\Db\Adapter\Adapter $dbAdapter
     */
    private function __construct(Adapter $dbAdapter) {
        $this->dbAdapter = $dbAdapter;
    }

    public static function getInstance($adapter) {
        if (self::$pensumService == null) {
            self::$pensumService = new PensumService($adapter);
        }
        return self::$pensumService;
    }

    public function getPensumDao() {
        return PensumDao::getInstance($this->dbAdapter);
    }
    
    public function getRegistro($id){
        $data = null;
        $success = false;
        $pensum = $this->obtenerPensumSegunId($id);
        if ($pensum->getId() != null) {
            $data[0] = $pensum->objectToArray();;
            $success = true;
            $mensaje = 'Pensum Encontrada';
        }
        $output = array(
            'success' => $success,
            'data' => $data,
            'total' => count($data)
        );
        return $output;
    }
    
    public function getRegistroCompleto($id) {
        $pensum = $this->obtenerPensumSegunId($id);
        return $this->armarRegistroCompleto($pensum);
    }
    public function getRegistroPensumSegunMateriaIdYMallaId($idmateria,$idmalla) {
        $pensum = $this->obtenerPensumSegunMateriaIdYMallaId($idmateria,$idmalla);
        return $this->armarRegistroCompleto($pensum);
    }
    
    public function armarRegistroCompleto($pensum) {
        $mensaje = 'Pensum no encontrado';
        $data = null;
        $success = false;
        $servicioMateria = MateriaService::getInstance($this->dbAdapter);
        $materia = $servicioMateria->getRegistroCompleto($pensum->getMateria());
        if ($pensum->getId() != null) {
            $data[0] = $pensum->objectToArray();
            $success = true;
            $data[0]['_materia'] = $materia['data'];
            $mensaje = 'Pensum Encontrado';
        }
        $output = array(
            'success' => $success,
            'data' => $data,    
            'total' => count($data),
            'mensaje' => $mensaje
        );
        return $output;
    }
    
    //------Retornan los objetos-------

    /**
     * Retorna un registro completo dado un id de malla de la tabla malla
     * @param $id type string
     * @return Objeto malla
     */
    public function obtenerPensumSegunId ($id){
        $registros = $this->getPensumDao()->getRegistro($id);
        $data = UtilsArrayController::llenarData($registros);
        $pensum = new Pensum($data[0]);
        $pensum->setId($data[0]['id']);
        $pensum->setMateria($data[0]['id_materia']);
        $pensum->setMalla($data[0]['id_malla']);
        return $pensum;
    }

    public function obtenerPensumSegunMateriaIdYMallaId($idmateria,$idmalla){
        $registros = $this->getPensumDao()->getPensumSegunMateriaIdYMallaId($idmateria,$idmalla);
        $data = UtilsArrayController::llenarData($registros);
        //print_r($data);
        $pensum = new Pensum($data[0]);
        $pensum->setId($data[0]['id']);
        $pensum->setMateria($data[0]['id_materia']);
        $pensum->setMalla($data[0]['id_malla']);
        //print_r($pensum);
        return $pensum;
    }
    
    
    //put your code here
}
